package com.gm.englishdemo3.ui.home;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gm.englishdemo3.R;
import com.gm.englishdemo3.adapter.HomeWorkAdapter;
import com.gm.englishdemo3.bean.HomeWorkBean;
import com.gm.englishdemo3.utils.OkHttpTool;
import com.gm.englishdemo3.utils.ServiceUrls;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HomeFragment extends Fragment {

    private RecyclerView homeWorkRy;

    private HomeWorkAdapter homeWorkAdapter=new HomeWorkAdapter();

    private Activity mActivityContext;
    @Override
    public void onAttach(@NonNull Context context) {
        this.mActivityContext=(Activity) context;
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_home,container,false);
        initView(view);

        //设置控件事件
        setViewListener(view);
        return view;
    }

    private void setViewListener(View view) {
    }

    private void initView(View view) {
        homeWorkRy=view.findViewById(R.id.comment_ry);
//==1、设置布局管理器
        //=1.1、创建布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        //=1.2、设置为垂直排列，用setOrientation方法设置(默认为垂直布局)
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //=1.3、设置recyclerView的布局管理器
        homeWorkRy.setLayoutManager(layoutManager);
        homeWorkRy.setAdapter(homeWorkAdapter);
        loadListData();

    }

    private void loadListData() {
        String url= ServiceUrls.getMainPageMethodUrl();
        Map<String,Object> map=new HashMap<>();
        OkHttpTool.httpGet(url, map, new OkHttpTool.ResponseCallback() {
            @Override
            public void onResponse(boolean isSuccess, int responseCode, String response, Exception exception) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (isSuccess && responseCode == 200) {
                            //请求成功
                            List<HomeWorkBean> pagination = new ArrayList<>();
                            //使用Gson对响应的数据反序列化
                            Type type = new TypeToken<List<HomeWorkBean>>() {
                            }.getType();
                            pagination = new Gson().fromJson(response, type);
                            //处理数据
                            if (pagination != null && !pagination.isEmpty()) {
                                //将分页数据添加到recyclerView的适配器
                                System.out.println("数据获取成功");
                                System.out.println(pagination);
                                homeWorkAdapter.setData(pagination);
                            } else {
                                System.out.println("数据获取失败");
                            }
                        } else {
                            System.out.println("数据获取失败");
                        }
                    }
                });
            }
        });
    }
}
